package org.jjliu.example.domain;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.FlushMode;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.AliasToBeanResultTransformer;
import org.hibernate.transform.Transformers;
import org.hibernate.type.LongType;
import org.jjliu.example.dao.HibernateDao;
import org.jjliu.example.entity.Admin;
import org.jjliu.example.entity.Article;
import org.jjliu.example.entity.Intro;
import org.jjliu.example.entity.Lock;
import org.jjliu.example.entity.Lock2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;


@Service
public class TestService {
    @Resource
    HibernateDao hibernateDao;
    
    public void deleteAdmin(){
		Admin ad = new Admin();
//		session.save(ad);
		Intro intro = new Intro();
		intro.setAdmin(ad);
		ad.setIntro(intro);
		hibernateDao.save(ad);
//		Admin ad = new Admin();
//		ad.setName("nima");
//		session.save(ad);
//    	Intro intro = new Intro();
//    	intro.setReMark("fuck");
//    	ad.setIntro(intro);
//    	intro.setAdmin(ad);
//    	session.save(intro);
//		session.delete(ad);
    }
    
    public void saveAdmin(){
    	Admin admin = new Admin();
    	admin.setName("woca");
    	Intro intro = new Intro();
    	intro.setReMark("fuck");
    	admin.setIntro(intro);
    	intro.setAdmin(admin);
    	hibernateDao.saveOrUpdate(admin);
    }

	public void querydb(){
		Session session = hibernateDao.getCurrentSession();
		List<Admin> adminList = session
				.createQuery("from Admin as a left join a.intro as i where i is null")
				.list();
//		List  adminList = session.createSQLQuery("select ad.id as {ad.id} ad.name as {ad.name} from admin as ad")
//				.addEntity("ad",Admin.class).setResultTransformer(Transformers.aliasToBean(Admin.class)).list();
			System.out.println(JSON.toJSONString(adminList, SerializerFeature.PrettyFormat));
	}
	
	public void createLock(){
		Session session = hibernateDao.getCurrentSession();
		Lock l = new Lock();
		l.setNum(100);
		session.save(l);
	}
	void sleep(long time){
		try {
			Thread.sleep(time);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	@Transactional
	public void lock1(){
		Session session = hibernateDao.getCurrentSession();
		
		Lock lock = (Lock) session.createCriteria(Lock.class).list().get(0);
		lock.setNum(200);
		session.flush();
		System.out.println("thread1 lock: "+lock.getNum());
		
		sleep(5000);
		
		Lock2 lock2 = (Lock2) session.createCriteria(Lock2.class).list().get(0);
		lock2.setNum(300);
		session.flush();
		System.out.println("thread1 lock2: "+lock2.getNum());
		
	}
	
	@Transactional
	public void lock2(){
		Session session = hibernateDao.getCurrentSession();
		
		Lock2 lock2 = (Lock2) session.createCriteria(Lock2.class).list().get(0);
		lock2.setNum(200);
		session.flush();
		System.out.println("thread2 lock2: "+lock2.getNum());
		
		sleep(5000);
		
		Lock lock = (Lock) session.createCriteria(Lock.class).list().get(0);
		lock.setNum(300);
		session.flush();
		System.out.println("thread2 lock: "+lock.getNum());
		
	}
	public void refreshLock(){
		Session session = hibernateDao.getCurrentSession();
		Lock lock = (Lock) session.createCriteria(Lock.class).list().get(0);
		System.out.println(lock.getNum());
		lock.setNum(100);
	}
	
	
}
